import React from 'react';
import {
  View,
} from 'react-native';

import CommonListPage from '../common/CommonListPage';
import UserNoticeChatItemView from './UserNoticeChatItemView';
import PostRequestMyPostCommentsAction from '../../actions/post/PostRequestMyPostCommentsAction';
import PostRequestMyPushPostsAction from '../../actions/post/PostRequestMyPushPostsAction';
import NewsRequestMyNewsAction from '../../actions/news/NewsRequestMyNewsAction';
import DataManager from '../../datas/DataManager';
import Constants from '../../common/Constants';
import PostConstants from '../../common/PostConstants';
import SizeUtils from '../../common/SizeUtils';
import TImage from '../../components/TImage';
import Config from '../../config';
import {DateUtils} from '../../utils/Utils';

export default class UserNoticeChatPage extends CommonListPage {
  constructor(props) {
    super(props);

    this._timeSave = [];
    if(this.props.pageData.routeData.noticeData.key === "quanzi_post" ||
      this.props.pageData.routeData.noticeData.key === "huiyuan" ||
      this.props.pageData.routeData.noticeData.key === "zahupu_post") {
      this._dataKey = "post_id";
    }
    else if(this.props.pageData.routeData.noticeData.key === "news") {
      this._dataKey = "news_id";
    }
  }

  _getListViewStyle() {
    return {
        width:SizeUtils.getWindowSize().width,
        height:(SizeUtils.getWindowSize().height - Config.paddingTop - SizeUtils.translateDesignHeightToCode(40)),
        backgroundColor:"#ffffff00"};
  }

  _requestData(page, listView) {
    if(this.props.pageData.routeData.noticeData.key === "quanzi_post") {
      new PostRequestMyPostCommentsAction().call(listView, {board_type:PostConstants.BORDER_TYPE.QUANZI, page:page});
    }
    else if(this.props.pageData.routeData.noticeData.key === "news") {
      new NewsRequestMyNewsAction().call(listView, {page:page});
    }
    else if(this.props.pageData.routeData.noticeData.key === "zahupu_post") {
      new PostRequestMyPostCommentsAction().call(listView, {board_type:PostConstants.BORDER_TYPE.ZAHUOPU, page:page});
    }
    else if(this.props.pageData.routeData.noticeData.key === "huiyuan") {
      new PostRequestMyPushPostsAction().call(listView, {board_id:2, page:page});
    }
  }

  _dealComponentWillUpdate(nextProps, nextState) {
    super._dealComponentWillUpdate(nextProps, nextState);
    this._timeSave = [];
  }

  _dealLeavePage() {
    super._dealLeavePage();
    this._timeSave = [];
  }

  _onRenderItem(data, index) {
    return <UserNoticeChatItemView data={data} noticeData={this.props.pageData.routeData.noticeData} index={index}
        checkShowTime={this._checkShowTime.bind(this)}/>
  }

  _checkShowTime(data) {
    if(this._timeSave.length === 0) {
      this._timeSave.push(data);
      return true;
    }
    else {
      var timeName = this._getTimeName();
      if(timeName === "") {
        return true;
      }
      let lastDate = DateUtils.getDate(this._timeSave[this._timeSave.length - 1][timeName]);
      let nowDate = DateUtils.getDate(data[timeName]);
      if(lastDate.yeah < nowDate.yeah || lastDate.month < nowDate.month
        || lastDate.day < nowDate.day || lastDate.hours < nowDate.hours
        || lastDate.minutes < (nowDate.minutes - 5)) {
          this._timeSave.push(data);
          return true;
        }
    }
    return false;
  }

  _getTimeName() {
    if(this.props.pageData.routeData.noticeData.key === "quanzi_post" ||
      this.props.pageData.routeData.noticeData.key === "zahupu_post") {
      return "commented_at";
    }
    else if(this.props.pageData.routeData.noticeData.key === "news" ||
      this.props.pageData.routeData.noticeData.key === "huiyuan") {
      return "posted_at";
    }
    return "";
  }

  //override
  _getTitle() {
    return this.props.pageData.routeData.noticeData.name;
  }
}
